import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin, UserConfig, loadEnv } from 'electron-vite'
import type { ConfigEnv } from 'vite'
import AutoImoprt from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import ElementPlus from 'unplugin-element-plus/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import vue from '@vitejs/plugin-vue'

export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  const root = process.cwd()

  const env = loadEnv(mode, root) as { VITE_APP_BASEURL: string; VITE_APP_DEV_URL: string }
  console.log(env)
  return {
    main: {
      plugins: [externalizeDepsPlugin()]
    },
    preload: {
      plugins: [externalizeDepsPlugin()]
    },
    renderer: {
      resolve: {
        alias: {
          '@renderer': resolve('src/renderer/src'),
          '@dts': resolve('src/renderer/src/dts'),
          '@utils': resolve('src/renderer/src/utils'),
          '@api': resolve('src/renderer/src/api'),
          '@views': resolve('src/renderer/src/views'),
          '@style': resolve('src/renderer/src/styles'),
          '@components': resolve('src/renderer/src/components'),
          '@router': resolve('src/renderer/src/router'),
          '@store': resolve('src/renderer/src/store'),
          '@assets': resolve('src/renderer/src/assets'),
          '@hooks': resolve('src/renderer/src/hooks')
        }
      },
      plugins: [
        vue(),
        //  开启ElementPlus自动引入CSS
        ElementPlus({}),
        AutoImoprt({
          resolvers: [ElementPlusResolver()],
          dts: resolve('src/types/auto-imports.d.ts')
        }),
        Components({
          resolvers: [ElementPlusResolver()],
          dts: resolve('src/types/components.d.ts')
        })
      ],
      server: {
        proxy: {
          '/api': {
            target: 'http://uat.crm.xuexiluxian.cn',
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api/, '')
          }
        }
      }
    }
  }
})
